import {
    Context,DomainModel,UserModel,PERM,TokenModel
} from 'hydrooj';


async function getTeacher(domainId: string) {
   if (!this.user.hasPerm(PERM.PERM_VIEW_RANKING)) return [1];
    const dudocs = await DomainModel.getMultiUserInDomain(domainId, { uid:{$gt: 1},role: "root"}) 
      .project({ uid: 1 }).toArray();
    
    // const [dudocs] = await this.paginate(
    //     DomainModel.getMultiUserInDomain(domainId, { uid:{$gt: 1},role: "root"})
    // );
    const uids = dudocs.map((dudoc) => dudoc.uid);
    this.collectUser(uids);
    const ustatus=dudocs.map((dudoc) => {
        TokenModel.getMostRecentSessionByUid(dudoc.uid, ['createAt', 'updateAt'])
    })
    
    const udict = await UserModel.getList(domainId, Array.from(uids));

    return [udict,dudocs];
    
}

export async function apply(ctx:Context){
    ctx.withHandlerClass('HomeHandler',(h)=>{
        h.prototype.getTeacher=getTeacher;
        //h.prototype.getRanking=getTeacher;
    });
}
